﻿// -----------------------------------------------------------------------
// <copyright file="Round_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Round_DAL:BaseDAL
    {
        
        // Lấy tất cả Round
        public static List<Round> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Rounds);
            return DB.Rounds.Where(n => n.Ship_ID == shipID).ToList();
        }
        // Lấy Round theo Id
        public static Round GetDataById(int id)
        {
            return DB.Rounds.Where(m => m.Round_ID == id).FirstOrDefault();
        }
        // Lấy Round theo Id
        public static Round GetDataByCode(string code, int shipID)
        {
            return DB.Rounds.Where(m => (m.RoundCode == code) && (m.Ship_ID == shipID)).FirstOrDefault();
        }
        //Thêm một Round
        public static bool Insert(Round Round)
        {
            try
            {
                DB.Rounds.InsertOnSubmit(Round);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Round
        public static bool Delete(Round Round)
        {
            try
            {
                DB.Rounds.DeleteOnSubmit(Round);
                DB.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }

        }
        // Cập nhật Round
        public static bool Update(Round Round)
        {
            try
            {
                //Round objRound = GetDataById(Round.Round_ID);
                //objRound.CodeRound = Round.CodeRound;
                //objRound.NameRound = Round.NameRound;
                //objRound.Address = Round.Address;
                //objRound.Tell = Round.Tell;
                //objRound.Email = Round.Email;
                //objRound.ContactPerson = Round.ContactPerson;
                //objRound.Fax = Round.Fax;
                //objRound.Note = Round.Note;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // Kiểm tra xem Round Code có tồn tại chưa ?
        public static bool IsExits(Round Round)
        {
            Round objRound = null;
            objRound = DB.Rounds.Where(m => (m.RoundCode == Round.RoundCode) &&
                (m.Round_ID != Round.Round_ID) && (m.Ship_ID == Round.Ship_ID)).FirstOrDefault();
            return objRound != null ? true : false;
        }
        public static bool IsRelationship(Round Round)
        {
            RoundJob roundJob = DB.RoundJobs.Where(n => n.Round_ID == Round.Round_ID).FirstOrDefault();
            if (roundJob != null)
                return false;

            WorkOrder workOrder = DB.WorkOrders.Where(n => n.Round_ID == Round.Round_ID).FirstOrDefault();
            if (workOrder != null)
                return false;

            return true;
        }       

    }
}
